<template>
    <orh-table pages :pageDate="pageDate" del="del" @on-del="confirm" @on-page-change="queryData" reload @on-reload="refresh" search  @on-search="searchmt" >
        <current-term @on-formstate="formState" style="width:200px" slot="option"></current-term>
        <Button type="primary" slot="option" style="margin-left: 10px;" :loading="butloading" @click="exportAll" v-button="'teacherleave_export'" size="small">导出教师请假数据</Button>
        <Button type="primary" slot="option" style="margin-left: 10px;" @click="exportSameDay" v-button="'teacherleavesame_export'" size="small">选择导出数据</Button>
		<Modal v-model="lookupState" slot="others">
			<p slot="header"><span>请选择时间段</span></p>
			<div style="text-align:center">
				<DatePicker type="daterange" v-if="lookupState" @on-change="seltime" placeholder="选择时间段" style="width: 200px"></DatePicker>
			</div>
			<div slot="footer">
				<Button type="primary" :loading="timeloading" @click="lookupSubmit">确定</Button>
				<Button @click="lookupState=false">取消</Button>
			</div>
		</Modal>
        <Table :columns="columns1" :data="tealeaveList" @on-selection-change="selectOne" slot="table" :loading="tableload"  highlight-row ref="currentRowTable"></Table>
        <orh-sidebar v-model="show" :title="layouttitle" scale slot="others">
            <component :is="content" :infoId='infoId' @requery="requery" :datas="datas"></component>
        </orh-sidebar>
    </orh-table>
</template>

<script>
export default {
    components: {
		TeacherLeaveDetail: resolve => {require(['./TeacherLeaveDetail.vue'], resolve);},
        TeacherLeaveEdit: resolve => {require(['./TeacherLeaveEdit.vue'], resolve);},
        CurrentTerm: resolve=> require(['&/Authpub/CurrentTerm.vue'],resolve),
		Echarts: () => import("&/Public/Echarts").then(m => m.default || m)
    },
    name: 'leave-teacherleave',
        data(){
            return {
                start_time:'',
                end_time:'',
                timeloading:false,
                lookupState:false,
                infoId:null,
                datas:null,
                content:null,
                butloading:false,
                semesterList:[],
                del:null,
                layouttitle:null,
                show:false,
                pageDate: {
                    total: 0,
                    current_page: 1,
                    per_page: 1
                },
                tealeaveList:[],
                tableload:true,
                listurl:null,
                delUrl:'api/leave/v1/teacher_leave/alldelete/',
                allid:null,
                columns1: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                    },
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '教师姓名',
                        render: (h, params) => {
                            return h('div', params.row.teacher!=null?params.row.teacher.teacher_name.zh:'');
                        }
                    },
                    {
                        title: '请假种类',
                        render: (h, params) => {
                            return h('div', params.row.teacher_leave_kinds.length != 0?params.row.teacher_leave_kinds[0].dictionary_name.zh:'');
                        }
                    },
                    {
                        title: '请假开始时间',
                        align: 'center',
                        key:'start_time',
                    },
                    {
                        title: '请假结束时间',
                        align: 'center',
                        key:'end_time',
                    },
                    {
                        title: '是否调课',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('div',params.row.whether_class == '1'?'是':'否')
                            ]);
                        }
                    },
                    {
                        title: '审核状态',
                        align: 'center',
                        render: (h, params) => {
                        let ss = params.row.examine_status
                        if(ss == '0'){
                            return h('div', [
                            h('div',{
                                style: {
                                color: '#007d7f',
                                },
                            }, '未审核')
                            ]);
                        }else if(ss == '1'){
                            return h('div', [
                            h('div',{
                                style: {
                                color: '#1aad33',
                                },
                            }, '通过')
                            ]);
                        }else if(ss == '2'){
                            return h('div', [
                            h('div',{
                                style: {
                                color: '#f00',
                                },
                            }, '未通过')
                            ]);
                        }else{
                            return h('div', [
                            h('div',{
                                style: {
                                color: '#FFC107',
                                },
                            }, '审核中')
                            ]);
                        }
                        }
                    },
                    {
                        title: '操作',
                        key: 'operation',
                        align: 'center',
                        width: 200,
                        render: (h, params) => {
                        return h('div', [
                            h('Button', {
                            props: {
                                type: 'success',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px',
                            },
                            on: {
                                click: () => {
                                this.see(params.row)
                                }
                            }
                        }, '查看'),
                        h('Button', {
                            directives: [
                                {
                                    name: 'button',
                                    value: 'edit',
                                }
                            ],
                            props: {
                                type: 'primary',
                                size: 'small',
                            },
                            style: {
                                marginRight: '5px',
                            },
                            on: {
                                click: () => {
                                    this.update(params.row.teacher_leave_id)
                                }
                            }
                        }, '编辑'),
                        h('Button', {
                            directives: [
                                {
                                    name: "button",
                                    value: "del"
                                }
                            ],
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px',
                            },
                            on: {
                                click: () => {
                                    this.del = [params.row.teacher_leave_id]
                                    this.confirm()
                                }
                            }
                        }, '删除'),
				    ]);
				}
			}
                ],
            }
        },

        methods: {
            // 获取教师请假的数据
            queryData(index){
                this.tableload = true
                if(this.allid!=undefined || this.allid!=null){
                    this.$http.get(`${this.listurl}${index}`).then(val=>{
                        this.tealeaveList = val.data.data
                        if(val.pages!=null){
                            this.pageDate.total=val.data.total
                            this.pageDate.per_page=val.data.per_page
                            this.pageDate.current_page=val.data.current_page
                        }
                        this.tableload = false
                    }).catch(err => {
                        if (err.code && err.code == 422) {
                            this.$Message.error(err.message);
                        }
                        this.tableload = false
                    })
                }else{
                    this.$Message.info('请选择学期');
                    this.tableload = false
                }
            },
            // 点击编辑时向子组件传数据
            update(index){
                this.infoId = index
                this.show = true
			    this.content = 'TeacherLeaveEdit'
                this.layouttitle='修改'
            },
            // 修改返回数据更新
            requery(val){
                if(val == true){
                    this.show = false
                    this.queryData(this.pageDate.current_page);
                }
            },
            see(data){
                this.show = true
			    this.content = 'TeacherLeaveDetail'
                this.datas = data;
                this.layouttitle='查看详情'
            },
            // 搜索操作
            searchmt(str) {
                this.listurl=`api/leave/v1/teacher_leave/index?searchvalue=${str}&page=`
                this.queryData(this.pageDate.current_page)
            },
            // 执行刷新操作
            refresh () {
                this.listurl = `api/leave/v1/teacher_leave/index?semester_id=${this.allid}&page=`
                this.queryData(this.pageDate.current_page);
            },
            /************************************************************
             * 批量删除
             ************************************************************/
            selectOne(index){
                this.del = index.map(item=> item.teacher_leave_id)
            },
            /************************************************************
             * 执行删除操作
             ************************************************************/
            confirm () {
                this.$Modal.confirm({
                    title: '数据删除',
                    content: '<p>您确定要删除这些数据吗?</p>',
                    onOk: () => {
                        if(this.del.length==0){
                            this.$Message.error('请选择删除项');
                            return
                        }
                        this.del = JSON.stringify(this.del)
                        this.tableload = true
                        this.$http.delete(`${this.delUrl}${this.del}`).then(val=>{
                            this.del=[]
                            this.refresh()
                            this.$Message.success(val.message);
                        }).catch(err=>{
                            if(err.code && err.code==422) {
                                this.$Message.error(err.message);
                            }
                            this.del=[]
                            this.tableload = false
                        })
                    },
                });
            },
            //选择学期
            formState(val){
                if(val!=undefined || val!=null){
                    this.allid = val
                    this.listurl = `api/leave/v1/teacher_leave/index?semester_id=${val}&page=`
                    this.queryData(1)
                }else{
                    this.tableload = false
                }
            },
            /************************************************************
            * 时间处理
            ************************************************************/
            seltime(val){
                this.start_time = val[0]
                this.end_time = val[1]
            },
            /************************************************************
            * 选择时间段导出数据
            ************************************************************/
            lookupSubmit(){
                this.lookupState = false
                this.publicexport()
            },
            //导出
            exportAll(){
                if(this.allid!=null){
                    this.butloading=true
                    this.$http.post(`api/leave/v1/leave_statistics/leave_teacher_out?semester_id=${this.allid}`).then(val => {
                        this.butloading=false
                        window.open(val.data.url)
                    });
                }else{
                    this.$Message.info('请选择学期！');
                }
            },
            /************************************************************
            * 选择导出数据
            ************************************************************/
            exportSameDay(){
                if(this.allid!=null){
                    this.lookupState = true
                    this.publicexport()
                }else{
                    this.$Message.info('请选择学期');
                }
            },
            /************************************************************
            * 选择导出数据的公共方法
            ************************************************************/
            publicexport(){
                if(this.start_time!='' && this.end_time!=''){
                    this.timeloading = true
                    this.$http.post(`api/leave/v1/leave_statistics/leave_teacher_out?semester_id=${this.allid}&start_time=${this.start_time}&end_time=${this.end_time}`).then(val => {
                        window.open(val.data.url)
                        this.start_time=''
                        this.end_time=''
                        this.timeloading = false
                    }).catch(err => {
                        this.timeloading = false
                    })
                }
            }
        },
        created(){
        },
}
</script>
